<template>
  <div class="add-comment">
    <van-field
      v-model.trim="value"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      plain
      type="info"
      size="small"
      :square="true"
      @click="addComment"
      >评论</van-button
    >
  </div>
</template>

<script>
import { comment } from '@/api/commend'
export default {
  name: 'AddComment',
  props: {
    // 文章或者评论ID
    target: {
      type: String,
      required: true
    },
    artId: {
      type: [String, Number],
      default: null
    }
  },
  data () {
    return {
      value: ''
    }
  },
  methods: {
    async addComment () {
      if (this.value === '') {
        this.value = ''
        return
      }
      try {
        const res = await comment({
          target: this.target,
          content: this.value,
          art_id: this.artId
        })
        this.$toast('评论成功')
        this.$emit('addComment', res.data.new_obj)
      } catch (err) {
        this.$toast('评论失败')
      }
    }
  }
}
</script>

<style scoped lang="scss">
.add-comment {
  display: flex;
  align-items: center;
}
.van-button {
  width: 60px;
  height: 50px;
}
</style>
